﻿using Alchemy;
using Alchemy.Classes;
using System;
using System.Collections.Generic;
using System.Net;

namespace shserver
{
    enum SH_Errors
    {
        ERROR_HOUND_NOT_FOUND = 100,
        ERROR_PACKET_HEADER_NOT_FOUND = 101,
        ERROR_WRITE_FAILED = 102,
        ERROR_WRONG_NUM_READ = 103,
        ERROR_READ_TIMEOUT = 104,
        ERROR_DEVICE_NOT_LOADED = 105,
        ERROR_MISSING_DATA = 106,
        ERROR_EXTRA_DATA = 107,
        ERROR_OUT_OF_RANGE = 200,
        ERROR_NO_EXT_REF = 201,
    };

    enum WS_Command : int
    {
        CONFIGURE = 1,
        RESET = 9,
    }

    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<int, string> SHErrors = new Dictionary<int, string>();

            foreach (SH_Errors t in Enum.GetValues(typeof(SH_Errors)))
            {
                SHErrors.Add((int)t, t.ToString());
            }

            /*
            SHErrors.Add(100, "ERROR_HOUND_NOT_FOUND");
            SHErrors.Add(101, "ERROR_PACKET_HEADER_NOT_FOUND");
            SHErrors.Add(102, "ERROR_WRITE_FAILED");
            SHErrors.Add(103, "ERROR_WRONG_NUM_READ");
            SHErrors.Add(104, "ERROR_READ_TIMEOUT");
            SHErrors.Add(105, "ERROR_DEVICE_NOT_LOADED");
            SHErrors.Add(106, "ERROR_MISSING_DATA");
            SHErrors.Add(107, "ERROR_EXTRA_DATA");
            SHErrors.Add(200, "ERROR_OUT_OF_RANGE");
            SHErrors.Add(201, "ERROR_NO_EXT_REF");
            */
            WebSocketServer wsServer = new WebSocketServer(81, IPAddress.Any)
            {
                OnReceive = OnReceive,
                OnSend = OnSend,
                OnConnect = OnConnected,
                OnConnected = OnConnect,
                OnDisconnect = OnDisconnect,
                TimeOut = new TimeSpan(0, 5, 0)
            };

            wsServer.Start();


        }

        static void OnConnect(UserContext ws)
        {
            Console.WriteLine("Client connected from: {0}", ws.ClientAddress);

            SignalHound sh = SignalHound.Instance;

            Console.WriteLine("SHAPI Initializating...");
            sh.init();
            Console.WriteLine("SHAPI Initializated");
        }

        static void OnConnected(UserContext ws)
        {
        }

        static void OnReceive(UserContext ws)
        {
            SignalHound sh = SignalHound.Instance;

            switch((int)ws.Data){
                case (int)WS_Command.CONFIGURE:
                    sh.configure();
                    Console.WriteLine("SHAPI Configured");
                    break;
                case (int)WS_Command.RESET:
                    sh.reset();
                    Console.WriteLine("SHAPI Reseted");
                    break;
            }
        }

        static void OnSend(UserContext ws)
        {
        }

        static void OnDisconnect(UserContext context)
        {
            Console.WriteLine("Client disconnected");
        }
    }
}
